package com.gao.Test;

import com.gao.domain.Student;
import java.util.ArrayList;
import java.util.Scanner;

/**
 * @Author: ShanYuqing
 * @Version: v1.0
 */
public class test {
    public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        go:
        while (true) {
            System.out.println("-----欢迎来到学生管理系统------");
            System.out.println("1.添加学生");
            System.out.println("2.删除学生");
            System.out.println("3.修改学生");
            System.out.println("4.查看学生");
            System.out.println("5.退出");
            System.out.println("请输入您的选择：");
            int choice = sc.nextInt();
            switch (choice) {
                case 1:
                    addStudent(list);
                    break;
                case 2:
                    deleteStudent(list);
                    break;
                case 3:
                    updateStudent(list);
                    break;
                case 4:
                    selectStudent(list);
                    break;
                case 5:
                    System.out.println("感谢您的使用，再见！");
                    break go;
                default:
                    System.out.println("您输入的序号有误");
                    break;
            }
        }
    }

    /*
    添加学生
     */
    private static void addStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学生学号：");
        String id = sc.next();
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            if (id.equals(stu.getId())) {
                System.out.println("您输入的学号已存在");
                return;
            }
        }
        System.out.println("请输入学生姓名：");
        String name = sc.next();
        System.out.println("请输入学生年龄：");
        int age = sc.nextInt();
        System.out.println("请输入学生生日");
        String birthday = sc.next();
        Student stu = new Student(id, name, age, birthday);
        list.add(stu);
        System.out.println("添加成功");
    }

    /*
    删除学生
     */
    private static void deleteStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        if (list.size() > 0) {
            System.out.println("请输入您要删除的学号：");
            String input = sc.next();
           int index = index(input,list);
           if(index==-1){
               System.out.println("没有找到该学生信息");
           }
           else{
               list.remove(index);
               System.out.println("删除成功");
           }
        } else {
            System.out.println("系统没有录入学生信息！");
        }
    }

    /*
    修改学生
     */
    private static void updateStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        if (list.size() > 0) {
            System.out.println("请输入您要更改学生信息的学生id");
            String id = sc.next();
            int index = index(id,list);
            if (index == -1) {
                System.out.println("系统不存在该学生信息");
            }else{
                Student stu = list.get(index);
                System.out.println("输入更改后的名字");
                String name = sc.next();
                System.out.println("请输入更改后的学生年龄：");
                int age = sc.nextInt();
                System.out.println("请输入更改后的学生生日");
                String birthday = sc.next();
                stu.setName(name);
                stu.setAge(age);
                stu.setBirthday(birthday);
                list.set(index,stu);
                System.out.println("修改成功");
            }
        } else {
            System.out.println("系统没有录入学生信息");
        }
    }

    /*
    查看学生
     */
    private static void selectStudent(ArrayList<Student> list) {
        if (list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                Student stu = list.get(i);
                System.out.println(stu.getId() + "---" + stu.getName() + "---" + stu.getAge() + "---" + stu.getBirthday());
            }
        } else {
            System.out.println("系统没有录入学生信息");
        }
    }
    /*
    查找索引
     */
    private static  int index(String id, ArrayList<Student> list){
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            if(stu.getId().equals(id)){
                return i;
            }
        }
        return -1;
    }
}
